package com.bocai.kelala.ui.travel.stroke.presenter;

import android.util.Log;

import com.bocai.kelala.bean.CheckLineBean;
import com.bocai.kelala.bean.MyGradOrderBean;
import com.bocai.kelala.bean.MyPublishStrokeBean;
import com.bocai.kelala.bean.OrderDetailBean;
import com.bocai.kelala.ui.travel.stroke.contract.MyStrokeContract;
import com.bocai.kelala.ui.travel.stroke.contract.PublishStrokeContract;
import com.bocai.kelala.util.AESUtils;
import com.bocai.kelala.util.RetryWithDelay;
import com.bocai.mybase.base.BaseResponse;
import com.bocai.mybase.net.Api;
import com.google.gson.Gson;

import javax.inject.Inject;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/6/28.
 */

public class MyStrokePresenter extends MyStrokeContract.presenter {

    private final MyStrokeContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public MyStrokePresenter(MyStrokeContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void myStrokePublish(String pageIndex, String pageSize) {
        api.myStrokePublish(pageIndex,pageSize).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    MyPublishStrokeBean bean = new Gson().fromJson(result,MyPublishStrokeBean.class);
                    view.myStrokePublish(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void myGrabOrder(String pageIndex, String pageSize) {
        api.myGrabOrder(pageIndex,pageSize).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    MyGradOrderBean bean = new Gson().fromJson(result,MyGradOrderBean.class);
                    view.myGradOrder(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void orderDetail(String orderId) {
        view.showLoading("");
        api.orderDetail(orderId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    OrderDetailBean bean = new Gson().fromJson(result,OrderDetailBean.class);
                    view.getOrderDetail(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void orderPay(String type,String orderId) {
        view.showLoading("");
        api.payType(type,orderId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = baseResponse.getData();
                    Log.e("result",result);
                    view.orderPaySuccess(result);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                    view.orderPaySuccess("");
                }
            }
        });
    }

    @Override
    public void cancelOrder(String orderId) {
        view.showLoading("");
        api.cancleOrder(orderId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    view.cancelOrderSuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void addDepartTime(String orderId, String time) {
        view.showLoading("");
        api.addDepartTime(orderId,time).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    view.addDepartTimeSuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
